import axios from 'axios'
import store from 'src/store'

axios.interceptors.request.use(
  config => {
    if (config.method === 'post') {
      // config.data = qs.stringify(config.data)
      config.headers['Content-Type'] = 'application/json'
    }
    // config.headers.Authorization = 'eyJhbGciOiJIUzUxMiJ9.eyJzdWJJZCI6IjEiLCJzdWIiOiJhZG1pbiIsImF1ZGllbmNlIjoid2ViIiwiYXV0aCI6WyJBVVRIX1NZU1RFTV9PUkciLCJBVVRIX1NZU1RFTV9VU0VSIiwiRVhBTV9GSUVMRE1BTkFHRSIsIlJPTEVfQk9TU19BRE1JTiIsIkFVVEhfU1lTVEVNX0FVVEhPUklUWSIsIkFVVEhfU1lTVEVNX0FETUlOIiwiQVVUSF9TWVNURU1fQVVUSF9NQU5BR0UiLCJBVVRIX1NZU1RFTV9ST0xFIiwiQVVUSF9CVVNJTkVTU19PUkRFUkNPU1QiXSwiY3JlYXRlZCI6MTUzODExNzU0NDE1MSwiZXhwIjoxNTM4NzIyMzQ0fQ.KWgLMSuOmcme8gkcrLyls-42xc0taotmDXfwzES_LUzDbO6Y46h2kS1fefT20aJl12q6xBt3GI1uXKlAeva6RQ'
    if (store.state.token) {
      config.headers.Authorization = `token ${store.state.token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  })

// http response 拦截器

axios.interceptors.response.use(
  response => {
    return response
  },
  error => {
    if (error.response) {
      switch (error.response.status) {
        case 401:
          // 返回401 清除token信息并跳转到登录页面
          // window.location.href = error.response.data
      }
    }
    // console.log(JSON.stringify(error));//console : Error: Request failed with status code 402
    return Promise.reject(error.response) // 返回接口返回的错误信息
  }
)

export default axios
